<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>计算器</title>
		<style>
			input {
				width: 60px;
				height: 38px;
				font-size: 20px;
			}
		</style>
	</head>
	<body>
		<table border="0" cellspacing="0" cellpadding="3">
			<tr>
				<td colspan="3"><input type="text" id="in"
						style="width: 204px; height: 32px; font-size: 20px; text-align: right;" /> </td>
				<td> <input type="button" value="←" onclick="deleteOne()" /></td>

			</tr>
			<tr>
				<td> <input type="button" value="C" onclick="deleteAll()" /></td>
				<td><input type="button" value="(" onclick="but(this)" /></td>
				<td><input type="button" value=")" onclick="but(this)" /></td>
				<td><input type="button" value="%" onclick="but(this)" /></td>
			</tr>
			<tr>
				<td><input type="button" value="7" onclick="but(this)" /></td>
				<td><input type="button" value="8" onclick="but(this)" /></td>
				<td><input type="button" value="9" onclick="but(this)" /></td>
				<td><input type="button" value="+" onclick="but(this)" /></td>
			</tr>
			<tr>
				<td><input type="button" value="4" onclick="but(this)" /></td>
				<td><input type="button" value="5" onclick="but(this)" /></td>
				<td><input type="button" value="6" onclick="but(this)" /></td>
				<td><input type="button" value="-" onclick="but(this)" /></td>
			</tr>
			<tr>
				<td><input type="button" value="1" onclick="but(this)" /></td>
				<td><input type="button" value="2" onclick="but(this)" /></td>
				<td><input type="button" value="3" onclick="but(this)" /></td>
				<td><input type="button" value="*" onclick="but(this)" /></td>
			</tr>
			<tr>
				<td><input type="button" value="0" onclick="but(this)" /></td>
				<td><input type="button" value="." onclick="but(this)" /></td>
				<td><input type="button" value="=" onclick="res()" /></td>
				<td><input type="button" value="/" onclick="but(this)" /></td>
			</tr>
		</table>
		<br>
		<script>
		Number.EPSILON=(function(){   //解决兼容性问题
		        return Number.EPSILON?Number.EPSILON:Math.pow(2,-52);
		      })();
			var aim = '';
			function but(eve) {
				
				aim=aim+eve.value;
				document.getElementById("in").value=aim;
			}
			
			function deleteOne(){
				aim=aim.substr(0,aim.length-1);
				document.getElementById("in").value=aim;
			}
			
			function deleteAll(){
				aim='';
				document.getElementById("in").value=aim;
			}
			function numberEq(a,b){
				return Math.abs(a-b)<Number.EPSILON;
			}
			function res(){
				var str=document.getElementById("in").value;
			try{
				var result=eval(str);
				if(result==Infinity){
					document.getElementById("in").value="ERROR";
					 return false;
				}
				if(numberEq(result,result.toFixed(1))){
					document.getElementById("in").value=result.toFixed(1);
					 return false;
				}
				document.getElementById("in").value=result;
			}catch(e){
				document.getElementById("in").value="ERROR";
			}finally{
				aim='';
			}
			}
		</script>
	</body>
</html>
